// pages/base_mine/recycle_order/detail.js

let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        qrcode_base64: '',
        number: '',
        address: '',
        address_describe: '',
        action_date: '',
        action_time: '',
        liaison_name: '',
        liaison_phone: '',
        create_time: '',
        genre: 0,
        genre_name: '',
        frequency_name: '',
        status: 0,
        status_name: '',
        staff_realname: '',
        staff_phone: '',
        can_cancel: false,
        remark: '',
        cancel_time: '',
        cancel_cause: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 处理传入参数
        this.data.id = parseInt(options.id);

        // 获取详情
        app.execute(function() {
            let page = app.getPage();
            page.getDetail(page);
        });

    },

    /**
     * 自定义函数--获取详情
     */
    getDetail: function(page) {
        wx.showLoading({
            title: '获取详情...',
        });
        wx.request({
            url: app.data.service_url + 'recycle_order_long/get_detail',
            header: {
                'Client-Token': app.data.client_token
            },
            method: 'POST',
            data: {
                id: page.data.id
            },
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        page.setData(response.data.data);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--拨打电话
     */
    callStaff: function(e) {
        wx.makePhoneCall({
            phoneNumber: this.data.staff_phone
        });
    },

    /**
     * 自定义函数--取消订单
     */
    cancelOrder: function() {
        let page = app.getPage();
        wx.showModal({
            title: '提示',
            content: '确定要撤销该订单吗？',
            success: function(e) {
                if (true === e.confirm) {
                    wx.showLoading({
                        title: '请等待...',
                    });
                    wx.request({
                        url: app.data.service_url + 'recycle_order_long/cancel',
                        header: {
                            'Client-Token': app.data.client_token
                        },
                        method: 'POST',
                        data: {
                            id: page.data.id
                        },
                        success: function(response) {
                            wx.hideLoading();
                            if (200 === response.statusCode) {
                                if (0 === response.data.errno) {
                                    wx.showToast({
                                        title: response.data.message,
                                    });
                                    setTimeout(function() {
                                        wx.reLaunch({
                                            url: '/pages/base_mine/index/index',
                                        });
                                    }, 1500);
                                } else {
                                    wx.showToast({
                                        title: response.data.message,
                                        icon: 'none'
                                    });
                                };
                            } else {
                                wx.showToast({
                                    title: '远程服务返回错误',
                                    icon: 'none'
                                });
                            };
                        },
                        fail: function() {
                            wx.hideLoading();
                            wx.showToast({
                                title: '连接远程服务失败',
                                icon: 'none'
                            });
                        }
                    });
                };
            }
        });
    }

});